<?php
class OrdersAction extends Action
{
	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(7,$perm_arr)){
			$this->error("无权操作");
		}
	}

	public function menu(){
		$this->display();
	}

	public function scan(){
		$Order=M('Orders');
		$map['ship_status']=0;
		$map['status']='active';
		$map['create_time']=array('egt',time()-3600*3);
		$map['ship_status']=0;
		$this->order=$Order->where($map)->order('create_time desc')->select();
		$this->display();
	}

	public function set(){
		$Order=M('Orders');
		$type=$this->_get('action');
		$map['order_id']=$this->_get('id');
		switch($type){
			case "finish":$data['status']='finish';break;
			case "dead":$data['status']='dead';break;
		}
		$Order->where($map)->save($data);
		echo '1';
	}

	public function index(){
		$Order=M('Orders');
		$Payment=M('Payment');
		$listnum=cookie('listnum');
		if(empty($listnum)){
			$listnum=10;
		}
		$oid=$this->_get('order_id');
		if(!empty($oid)){
		    $map['order_id']=$oid;
	    }
		$type=$this->_get('type');
		switch($type){
			case 'all':;break;
			case 'new':$map['e_orders.status']="active";$map['e_orders.ship_status']=0;break;
			case 'payed':$map['e_orders.status']="active";$map['e_orders.pay_status']=1;$map['e_orders.ship_status']=0;break;
			case 'shipping':$map['e_orders.status']="active";$map['e_orders.ship_status']=1;break;
			case 'finish':$map['e_orders.status']="finish";break;
			case 'dead':$map['e_orders.status']="dead";break;
		}
		$mintime=$this->_get('mindate');
		$maxtime=$this->_get('maxdate');
		if(!empty($mintime)){
			$min=strtotime($mintime);
			$this->assign('mindate',$mintime);
		}else{
			$min=strtotime('2010-01-01');
		}
		if(!empty($maxtime)){
			$max=strtotime($maxtime.' 23:59');
			$this->assign('maxdate',$maxtime);
		}else{
			$max=time();
		}
		$map['e_orders.create_time']=array(array('egt',$min),array('elt',$max),'and');
		
		import("ORG.Util.Page");
		$count=$Order->where($map)->order('create_time DESC')->count();//->where($map)
		$Page=new Page($count,$listnum);

		$order=$Order->where($map)->join('e_payment ON e_orders.payment=e_payment.id')->order('e_orders.create_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();

		$Page->setConfig('header', '条');
		$Page->setConfig('prev', '<img src="__APP__/statics/up.gif">');
		$Page->setConfig('next', '<img src="__APP__/statics/down.gif">');
		$Page->setConfig('theme', "%upPage%  %linkPage% %downPage%  共%totalPage%页");
        $page = $Page->show();
		$this->order=$order;
		$this->assign("page", $page);
		$this->assign('listnum',$listnum);
		$this->display();
	}

	//发货单详情
	public function doc_ship(){
		$Ship=M('Order_ship');
		$Express=M('Express');
		$map['e_order_ship.order_id']=$this->_get('order');
		$ship=$Ship->join("e_express ON e_order_ship.ex_id=e_express.id")->where($map)->find();
		echo "订单：[".$map['e_order_ship.order_id']."]<br>";
		echo "配送：[".$ship['ex_name']."]<br>";
		if(!empty($ship['express_id'])){
			echo "单号：<font color='red'>".$ship['express_id']."</font><br>";
		}
		echo "时间：[".date('Y-m-d H:i:s',$ship['time'])."]<br>";
		echo "处理：[{$ship['op_id']}]<br>";
	}

	//订单支付历史
	public function doc_pay(){
		$Pay=M('Order_pay');
		$Payment=M('Payment');
		$map['e_order_pay.order_id']=$this->_get('order');
		$pay=$Payment->join("e_order_pay ON e_order_pay.payment=e_payment.id")->where($map)->order("pay_time desc")->select();
		for($i=0;$i<count($pay);$i++){
		    echo date("Y-m-d H:i:s",$pay[$i]['pay_time'])."【{$pay[$i]['pay_name']}:{$pay[$i]['payed']}】【{$pay[$i]['pay_id']}】<br>";
	    }
	}

	//优惠券使用记录
	public function doc_coupons(){
		$Order=M('Orders');
		$map['order_id']=$this->_get('order');
		$order=$Order->where($map)->find();
		if($order['pmt_name']!=null){
			echo "此单使用优惠券：<br>卡密（".implode('-',str_split($order['pmt_name'],4))."）<br>抵消金额：￥".$order['pmt_money'];
		}else{
			echo "未使用优惠";
		}
	}

    //订单详情
	public function detail(){
		$Order=M('Orders');
		$Item=M('Orders_item');
		$map['order_id']=$this->_get('order');
		$order_info = $Order->where($map)->find();

		$option['member_id'] = $order_info['member_id'];
		$this->user = MemberModel::getUserInfo($option);
		$this->orderlist=$Item->where($map)->select();
		$this->order=$order_info;
		$this->display();
	}

	//支付处理
	public function setpay(){
		$Order=M('Orders');
		$Payment=M('Payment');
		$map['order_id']=$this->_get('order');
		$type=$this->_get('action');
		$order=$Order->where($map)->find();

		if($type=='pay'){
			$order['npay']=$order['final_money']-$order['payed'];
			$this->order=$order;
			$this->payment=$Payment->select();
			$this->display();
		}else if($type=='fpay'){
			$Paydoc=M('Order_pay');
			$Log=M('Order_log');
			$nmoney=$this->_post('money');
			$data2['payed']=$order['payed']+$nmoney;
			$data2['last_time']=time();
			if($data2['payed']==$order['final_money']){
				$data2['pay_status']=1;
			}
			//更新订单表orders
			$re=$Order->where($map)->save($data2);

			$data4['order_id']=$map['order_id'];
			$data4['payment']=$this->_post('payment');
			$data4['payed']=$nmoney;
			$data4['pay_time']=time();
			$data4['pay_id']=session('admin_name');

			if($re){
			    $data4['result']='success';
			}else{
				$data4['result']='faile';
			}
			$re2=$Paydoc->add($data4);

			$data3['order_id']=$map['order_id'];
			$data3['op_name']=session('admin_name');
			$data3['log_text']="订单:".$map['order_id']."付款".$nmoney."元";
			$data3['acttime']=time();
			$data3['behavior']="订单支付";
			$data3['result']=$data4['result'];
			$Log->add($data3);
			if($re&&$re2){
				echo "<script>window.parent.location.reload()</script>";
			}else{
				$this->error("支付失败！");
			}
		}else{
			$ths->error("非法操作！");
		}
	}

	//发货处理
	public function setship(){
		$Order=M('Orders');
		$Express=M('Express');
		
		$map['order_id']=$this->_get('order');
		$type=$this->_get('action');
	    if($type=='ship'){
			$this->order=$Order->where($map)->find();
			$this->express=$Express->select();
			$this->display();
		}else if($type=='fship'){
			$Ship=M('Order_ship');
		    $Log=M('Order_log');
			//发货单生成
			$data['order_id']=$map['order_id'];
			$data['ex_id']=$this->_post('shipping');
			$data['express_id']=$this->_post('code');
			$data['ship_fee']=$this->_post('exfee');
			$data['note']=$this->_post('note');
			$data['time']=time();
			$data['op_id']=session('admin_name');

			$re=$Ship->add($data);

			$data2['order_id']=$map['order_id'];
			$data2['op_name']=session('admin_name');
			$data2['log_text']="订单:".$map['order_id']."发货处理";
			$data2['acttime']=time();
			$data2['behavior']="订单发货";
			
			if($re){
				$data2['result']='success';
				$data3['ship_status']=1;
				$Order->where($map)->save($data3);
				$Log->add($data2);
				echo "<script>window.parent.location.reload()</script>";
			}else{
				$data2['result']='failure';
				$Log->add($data2);
				$this->error("发货失败！请重试");
			}
		}else{
			$ths->error("非法操作！");
		}
	}
	
	public function setnum(){
		$num=$this->_get('num');
		if(empty($num)){
			$num=10;
		}
		cookie('listnum',$num,2592000);
		echo 1;
	}

	public function finishAll()
	{
		$order_ids = $this->_post('order_id');
		$res = OrderModel::finishOrders($order_ids);
		echo 1;
	}

	public function deadAll()
	{
		$order_ids = $this->_post('order_id');
		$res = OrderModel::deadOrders($order_ids);
		echo 1;
	}

}
?>